package com.ruoyi.project.xkx.school.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.project.xkx.school.domain.XkxClassStudent;
import com.ruoyi.project.xkx.school.mapper.XkxClassStudentMapper;
import com.ruoyi.project.xkx.school.service.IXkxClassStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.xkx.school.mapper.XkxClassMapper;
import com.ruoyi.project.xkx.school.domain.XkxClass;
import com.ruoyi.project.xkx.school.service.IXkxClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 所有班级Service业务层处理
 *
 * @author ..
 * @date 2021-05-11
 */
@Service
public class XkxClassServiceImpl extends ServiceImpl<XkxClassMapper, XkxClass> implements IXkxClassService {
  @Autowired
  private XkxClassMapper xkxClassMapper;
  @Autowired
  private IXkxClassStudentService classStudentService;
  @Autowired
  private XkxClassStudentMapper classStudentMapper;

  /**
   * 查询所有班级
   *
   * @param classId 所有班级ID
   * @return 所有班级
   */
  @Override
  public XkxClass selectXkxClassById(Integer classId) {
    return xkxClassMapper.selectXkxClassById(classId);
  }

  /**
   * 查询所有班级列表
   *
   * @param xkxClass 所有班级
   * @return 所有班级
   */
  @Override
  public List<XkxClass> selectXkxClassList(XkxClass xkxClass) {
    return xkxClassMapper.selectXkxClassList(xkxClass);
  }

  /**
   * 新增所有班级
   *
   * @param xkxClass 所有班级
   * @return 结果
   */
  @Override
  public int insertXkxClass(XkxClass xkxClass) {
    return xkxClassMapper.insertXkxClass(xkxClass);
  }

  /**
   * 修改所有班级
   *
   * @param xkxClass 所有班级
   * @return 结果
   */
  @Override
  public int updateXkxClass(XkxClass xkxClass) {
    return xkxClassMapper.updateXkxClass(xkxClass);
  }

  /**
   * 批量删除所有班级
   *
   * @param classIds 需要删除的所有班级ID
   * @return 结果
   */
  @Override
  public int deleteXkxClassByIds(Integer[] classIds) {
    return xkxClassMapper.deleteXkxClassByIds(classIds);
  }

  /**
   * 删除所有班级信息
   *
   * @param classId 所有班级ID
   * @return 结果
   */
  @Override
  public int deleteXkxClassById(Integer classId) {
    return xkxClassMapper.deleteXkxClassById(classId);
  }

  @Override
  public List<XkxClass> getSchoolClassStudent(Integer schoolId, Integer classId) {
    return xkxClassMapper.getSchoolClassStudent(schoolId, classId);
  }

  @Override
  @Transactional
  public Map classAttendance(Integer classId) {
    HashMap<String, Object> res = new HashMap<>();
    List<XkxClassStudent> classStudents = classStudentService.list(new QueryWrapper<XkxClassStudent>()
        .eq("class_id", classId).gt("course_hour", 0));
    Integer count = 0;
    if (classStudents.size() > 0) {
      XkxClass xkxClass = xkxClassMapper.selectById(classId);
      //默认消除课时
      Integer classDeductHours = xkxClass.getClassDeductHours();
      for (XkxClassStudent classStudent : classStudents) {
        classStudent.setCourseHour(classStudent.getCourseHour() - classDeductHours);
        int i = classStudentMapper.updateById(classStudent);
        if (i > 0) {
          count += 1;
        }
      }
      XkxClass xkxClass1 = new XkxClass();
      xkxClass1.setClassId(classId);
      xkxClass1.setAttendance(1);
      xkxClassMapper.updateById(xkxClass1);
    }
    int stuCount = classStudentService.count(new QueryWrapper<XkxClassStudent>().eq("class_id", classId));
    res.put("studentCount", stuCount);
    res.put("updateSuccessCount", count);
    return res;
  }

  @Override
  public void resetClassHour() {
    xkxClassMapper.resetClassHour();

  }

  @Override
  public List<XkxClass> getListClassByTeacherId(Integer teacherId) {
    return xkxClassMapper.getListClassByTeacherId(teacherId);
  }

  @Override
  public XkxClass getClassInfo(Integer classId) {


    return xkxClassMapper.getClassInfo(classId);
  }
}
